package com.jar.exercise.io.管道流;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * IO流中涉及到多线程的流
 * PipedInputStream,PipedOutputStream
 */
public class Demo {
	public static void main(String[] args) throws IOException {
		PipedOutputStream pos = new PipedOutputStream();
		PipedInputStream pis = new PipedInputStream();
		pis.connect(pos);
		new Thread(new Read(pis)).start();
		new Thread(new Writer(pos)).start();
	}
}

class Read implements Runnable {
	private PipedInputStream pis;

	public Read(PipedInputStream pis) {
		this.pis = pis;
	}

	@Override
	public void run() {
		try {
			byte[] b = new byte[1024];
			int len = pis.read(b);
			System.out.println(new String(b, 0, len));
			pis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

class Writer implements Runnable {
	private PipedOutputStream pos;

	public Writer(PipedOutputStream pos) {
		this.pos = pos;
	}

	@Override
	public void run() {
		try {
			pos.write("你好呀。撒旦发射点".getBytes());
			pos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
